#include "rxbit_parser.h"

namespace os
{
    namespace v2x
    {
        namespace device
        {

            void RxbitParser::Invoke(const u_char *data, int32_t size)
            {
                // 拷贝数据，追加在尾巴上
                memcpy(parser_buffer + parser_buffer_valid_count, data, size);
                parser_buffer_valid_count += size;
                parser_buffer_location = 0;

                // 大于最小包
                if (parser_buffer_valid_count >= PACKAGE_MIN_LEN)
                {
                    Format();
                }
            }

            void RxbitParser::Format()
            {
                while (true)
                {
                    printf("parser_buffer_location:%d, parser_buffer_valid_count:%d.\n", parser_buffer_location, parser_buffer_valid_count);
                    u_char *curPtr = (parser_buffer + parser_buffer_location);

                    // 解析
                    uint32_t header = *reinterpret_cast<const uint32_t *>(curPtr);
                    if (header == PACKAGE_HEADER_VALUE)
                    {
                        uint16_t dataLen = *reinterpret_cast<const uint16_t *>(curPtr + sizeof(header));

                        if (parser_buffer_valid_count >= dataLen)
                        {
                            uint32_t tailer = *reinterpret_cast<const uint32_t *>(curPtr + dataLen - sizeof(uint32_t));

                            if (tailer == PACKAGE_TAILER_VALUE)
                            {
                                // 数据正确
                                if (msgUpdate)
                                {
                                    // 数据反馈
                                    msgUpdate(curPtr, dataLen, FD);
                                }

                                parser_buffer_location += dataLen;
                                parser_buffer_valid_count -= dataLen;
                            }
                            else
                            {
                                // 整包数据丢掉
                                parser_buffer_location += dataLen;
                                parser_buffer_valid_count -= dataLen;
                            }
                        }
                        else
                        {
                            // 数据拷贝，退出解析
                            if (parser_buffer_valid_count > 0)
                            {
                                memcpy(parser_buffer, parser_buffer + parser_buffer_location, parser_buffer_valid_count);
                            }
                            parser_buffer_location = 0;
                            break;
                        }
                    }
                    else
                    {
                        parser_buffer_location += 4;
                    }

                    printf("end--parser_buffer_location:%d, parser_buffer_valid_count:%d.\n", parser_buffer_location, parser_buffer_valid_count);

                    // 跑到尾巴都没找到头
                    if (parser_buffer_location >= parser_buffer_valid_count)
                    {
                        parser_buffer_location = 0;
                        parser_buffer_valid_count = 0;
                        break;
                    }
                    if (parser_buffer_valid_count <= 0)
                    {
                        parser_buffer_valid_count = 0;
                        parser_buffer_location = 0;
                        break;
                    }
                }
            }

        } // namespace device
    }     // namespace v2x
} // namespace os